package mysql

import (
	"doctor-medicine-system/doctorrpc/doctorrpc"

	"gorm.io/gorm"
)

type Medicine struct {
	gorm.Model
	Name     string `gorm:"type:varchar(30);not null;comment:药品名称"`
	GuiGe    string `gorm:"type:varchar(30);not null;comment:规格"`
	ShangJia string `gorm:"type:varchar(300);not null;comment:商家"`
	ShiZheng string `gorm:"type:varchar(30);not null;comment:适应症状"`
	Num      int64  `gorm:"type:int(11);not null;comment:用量"`
	DoctorId int64  `gorm:"type:int(11);not null;comment:医生ID"`
	Types    string `gorm:"type:varchar(30);not null;comment:分类"`
}

func (m *Medicine) MedicineFindByName(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).FirstOrCreate(m).Error
}

func (m *Medicine) MedicineList(db *gorm.DB, id int64) []*doctorrpc.DoctorList {
	//SELECT
	//  node,
	//  medicines.`name`,
	//  gui_ge,
	//  shang_jia,
	//  shi_zheng,
	//  num,
	//  types
	//FROM
	//  doctors
	//  LEFT JOIN medicines ON doctors.id = medicines.doctor_id
	var list []*doctorrpc.DoctorList
	db.Debug().Table("medicines").Select("medicines.name as name, medicines.gui_ge as gui_ge,medicines.shang_jia as shang_jia,medicines.shi_zheng as shi_zheng,medicines.num as num,medicines.types as types ,node as node").
		Joins("LEFT JOIN doctors ON doctors.id = medicines.doctor_id").Where("doctors.id=?", id).Find(&list)
	return list
}
